package world.zwq.gulimall.search.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import world.zwq.common.exception.BaseCodeEnum;
import world.zwq.common.to.es.SkuEsModel;
import world.zwq.common.utils.R;
import world.zwq.gulimall.search.service.ProductSaveService;

import java.io.IOException;
import java.util.List;

@Slf4j
@RequestMapping("/search")
@RestController
public class ElasticsearchSaveController {

    @Autowired
    private ProductSaveService productSaveService;

    /**
     * 上架商品
     * @param skuEsModelList
     * @return
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModelList) {
        try {
            boolean b = productSaveService.productStatusUp(skuEsModelList);
            if(b){
                return R.error(BaseCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BaseCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
            }else{
                return R.ok();
            }
        } catch (IOException e) {
            log.error("商品上架错误 ElasticsearchSaveController.productStatusUp(),{}",e);
            return R.error(BaseCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BaseCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }

    }

}
